﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LoadingView : MonoSingleton<LoadingView>
{
    private bool m_IsShow;
    private bool m_IsHide;
    private bool m_IsOpenView;
    private bool m_IsWaitLoad;
    private bool m_IsWaitClose;
    private bool m_IsCloseView;

    private float aph;
    private float showSpeed = 4f;
    private float hideSpeed = 4f;
    private GameObject panel_loading;
    private Material Transfer;

    private void Update()
    {
        if (m_IsShow)
        {
            if (aph != 1)
            {
                aph = Mathf.Min(aph + Time.deltaTime * showSpeed, 1);
                LoadingEvents.WaitCloseView = false;
            }
            else if (m_IsWaitLoad)
            {
                m_IsWaitLoad = false;            
                AsynLoading.LoadScene();
            }
            else if (m_IsCloseView)
            {
                if (m_IsWaitClose)
                {
                    m_IsWaitClose = false;
                    LoadingEvents.HasOpenLoad = false;
                }
                m_IsHide = true;
                m_IsShow = false;
                m_IsCloseView = false;
                LoadingEvents.WaitCloseView = false;
            }
            else
            {
                LoadingEvents.WaitCloseView = true;
            }
        }
        else if (m_IsHide)
        {
            if (aph != 0)
            {
                aph = Mathf.Max(aph - Time.deltaTime * hideSpeed, 0);
            }
            else
            {
                m_IsHide = false;
                m_IsOpenView = false;
                enabled = false;
                if (panel_loading != null)
                    panel_loading.SetActive(false);

            }
        }
        Transfer.color = new Color(0, 0, 0, aph);
    }

    private void StartLoadView()
    {
        if (m_IsOpenView)
        {
            m_IsShow = true;
            m_IsHide = false;
        }
        else
        {
            m_IsOpenView = true;
            aph = 0;
            if (panel_loading != null)
                panel_loading.SetActive(true);
            m_IsShow = true;
            m_IsHide = false;
            enabled = true;
        }
    }

    public void CloseLoadView()
    {
        if (LoadingEvents.IsGradient)
        {
            if (m_IsOpenView)
                m_IsCloseView = true;
        }
    }

    public void OpenLoadingScene()
    {
        m_IsWaitClose = false;
        if (!LoadingEvents.HasOpenLoad)
        {
            if (LoadingEvents.IsGradient)
            {
                m_IsWaitLoad = true;
                StartLoadView();
            }
            else
            {
                LoadingEvents.WaitCloseView = true;
                AsynLoading.LoadScene();
            }
            LoadingEvents.HasOpenLoad = true;
        }
       
    }

    public void CloseLoadingScene()
    {
        m_IsWaitLoad = false;
        if (LoadingEvents.HasOpenLoad)
        {
            if (LoadingEvents.IsGradient)
            {
                m_IsWaitClose = true;
                m_IsCloseView = false;
                StartLoadView();
            }
            else
            {
                LoadingEvents.WaitCloseView = true;             
            }
            LoadingEvents.HasOpenLoad = false;
        }
       
    }

    public override void OnInit()
    {
        GameObject clone = Instantiate(Resources.Load<GameObject>("LoadingObj"), transform);
        clone.transform.SetParent(transform);
        clone.transform.localPosition = Vector3.one * 10000;
        panel_loading = clone.transform.GetChild(0).gameObject;
        Transfer = panel_loading.transform.Find("Transfer").GetComponent<Renderer>().material;
        enabled = false;
    }
}
